package com.scenic.userservice.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scenic.common.bean.Comment;
import com.scenic.common.exception.ScenicSpotException;
import com.scenic.common.service.CommentService;
import com.scenic.userservice.mapper.CommentMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Integer insert(Comment comment) {
        commentMapper.insert(comment);
        return comment.getId();
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws ScenicSpotException {
        //判断这个评论是否存在
        Comment comment = commentMapper.selectById(id);
        if (comment == null) {
            throw new ScenicSpotException("该评论不存在，无法删除");
        }
        return commentMapper.delete(id) ==1;
    }

    @Override
    public boolean update(Comment comment, Integer userId) throws ScenicSpotException {
        //判断这个评论是否存在
        Comment a = commentMapper.selectById(comment.getId());
        if (a == null) {
            throw new ScenicSpotException("该评论不存在，无法修改");
        }
        return commentMapper.update(comment) ==1;
    }

    @Override
    public List<Comment> selectByCondition(Comment condition) {
        return commentMapper.selectByCondition(condition);
    }

    @Override
    public PageInfo<Comment> selectByPage(Integer pageNum, Integer pageSize, Comment condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> list = commentMapper.selectByCondition(condition);
        PageInfo<Comment> PageInfo = new PageInfo<>(list);
        return PageInfo;
    }

    @Override
    public Comment selectById(Integer id) {
        return commentMapper.selectById(id);
    }
}
